require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

require(['jquery', 'doT', 'common', 'bootstrap', 'paginator','select2', 'datetimepicker'], function($, doT, common) {
    $(function() {
        var pageSize = 10;
        var tradeList;
        var page = 1;
        var pageNumber;

        var role=sessionStorage.getItem('role');
        if(role==1){
            $('.managerOnly').removeClass('managerOnly');
        }
        $('.datepicker').datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            maxView: 'decade',
            minView: 'month'
        });
        function getSchoolList() {
            $.getJSON(common.urlBase + '/school/allSchool.json', function(result) {
                var schoolTmpl = doT.template($('#schoolTmpl').text());
                $('#schoolSelect').html(schoolTmpl(result.data));
                $('#schoolSelect').select2();
            });
        }
        getSchoolList();

        $('#tradeForm').on('submit', function(event) {
            page = 1;
            event.preventDefault();
            refreshTradeList(1);
            var sid;
            if (sessionStorage.getItem('school_id')) {
                sid = sessionStorage.getItem('school_id');
            } else {
                sid = $('#schoolSelect').val();
            }
            $.getJSON(common.urlBase + '/shop/school-shop/' + sid + '.json', function(result) {
                if (result.ok) {
                    var shopTmpl = doT.template($('#shopTmpl').text());
                    $('#shopSelect').html(shopTmpl(result.data));
                } else {
                    $('#noticeMsg').text('该学校还未添加饭堂，请先添加饭堂!');
                    $('#noticeModal').modal('show');
                }
            });
        });

        function refreshTradeList(page) {
            var data = {};
            if (sessionStorage.getItem('school_id')) {
                data.school_id = sessionStorage.getItem('school_id');
            } else {
                data.school_id = $('#schoolSelect').val();
            }
            data.start_time = new Date($('#tradeDate').val()).getTime();
            data.end_time = common.addDay(data.start_time, 1);
            data.page = page;
            data.page_sise = pageSize;
            console.log(data);
            $.post(common.urlBase + '/transaction/trades/get', data, function(result) {
                if (result.ok && result.data.obj.length > 0) {
                    tradeList = result.data.obj;
                    refreshPaginator(result.data.total);
                    var tradeTmp = doT.template($('#tradeTmpl').text());
                    $('#tradeTable').html(tradeTmp(tradeList));
                    $('#tradeAlert').parent('.alert').hide();
                } else if (result.ok && result.data.obj.length === 0) {
                    $('#tradeAlert').text('暂无交易记录导入！');
                    $('#tradeAlert').parent('.alert').show();
                    $('#tradeTable').html('');
                    $('.paginatorRow').hide();
                } else {
                    $('#tradeAlert').text('查询出错，请重试');
                    $('#tradeAlert').parent('.alert').show();
                    $('#tradeTable').html('');
                    $('.paginatorRow').hide();
                }
            }, 'json');
        }

        function pageChange(event, oldPage, newPage) {
            refreshTradeList(newPage);
            page = newPage;
        }

        function refreshPaginator(total) {
            $('#total').text(total);
            pageNumber = Math.ceil(total / pageSize);
            common.initPaginator($('#paginator'), page, 8, pageNumber, pageChange);
            $('.paginatorRow').show();
        }

    });

});
